package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public  class DataActionTrack extends DataAction implements ITag{
	public static String DEFAULT_ID="_ActionTrack";
		DataPoints track = new DataPoints();
		DataPoint position = new DataPoint();
public DataActionTrack(DataNode parent) {
	super(parent);

	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml)
{

	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.TRACK))
		{
			track = new DataPoints();
	  		track.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.POSITION))
		{
			position = new DataPoint(TagModifier.POINT_TYPE_POINT);
	  		position.buildFromgrfxML(element,TagModifier.POINT_TYPE_POINT);
		}
		else if(super.buildFromElement(element)==false && !NameHelper.isSystemName(element.getName()))
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.ACTION_TRACK,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml)
{
	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.TRACK))
		{
			track.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.POSITION))
		{
			position.copyFromgrfxML(element,TagModifier.POINT_TYPE_POINT);
		}
		else if(super.copyFromElement(element)==false)
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.ACTION_MOVE,""+element.getStartLineNo()} );
		}
	}
}
public DataAction getClone() {

	DataActionTrack newActionTrack = new DataActionTrack(parent);

	super.getClonedElements(newActionTrack);

	newActionTrack.track = track.getClone();
	newActionTrack.position = position.getClone();

	return newActionTrack;
}
public DataAction getClone(String path) {

	DataActionTrack newActionTrack = new DataActionTrack(parent);

	GrfxMLStoreProcessor.setCloneName( path,this,newActionTrack );

	super.getClonedElements(path,newActionTrack);

	newActionTrack.track = track.getClone(path);
	newActionTrack.position = position.getClone(path);

	return newActionTrack;
}
public DataPoint getPosition() {
	return position;
}
/**
 *
 * @return org.one.stone.soup.grfxML.DataPoints
 */
public DataPoints getTrack() {
	return track;
}
/**
 * process method comment.
 */
protected void process()
{
 try{
	if(getTrack().size()==0)
		return;
	getSteps().setValue(getTrack().size(),this);
	if(getCurrentStep().getValue()>=getSteps().getValue())
		getCurrentStep().setValue(0,this);
	GrfxMLProcessor.copy(getTrack().get( getCurrentStep().getValue() ) , position);
 }catch(LockException le){}
}
public void register(DataLinker store) {

	super.register(store);

	try{
		store.add(track,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(position,this);
	}
	catch(LockException le)
	{}//Dummy catch
}
public void replace(Field oldObj, Field newObj) {

	replace(oldObj,newObj,false);

}
public void replace(Field oldObj, Field newObj,boolean recursive) {

	super.replace(oldObj,newObj);

	if(track==oldObj)
		track=(DataPoints)newObj;

	if(position==oldObj)
		position=(DataPoint)newObj;
}
public void resetChanged() {
	super.resetChanged();
	track.resetChanged();
	position.resetChanged();
}
protected void setPosition(DataPoint newPosition) {
	position = newPosition;
}
/**
 *
 * @param newTrack org.one.stone.soup.grfxML.DataPoints
 */
protected void setTrack(DataPoints newTrack) {
	track = newTrack;
}
public String togrfxML(String name) {
	String xml = "<" + name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml += ">";
	xml += super.togrfxML(name);
	xml += track.togrfxML(Token.TRACK);
	xml += position.togrfxML(Token.POSITION);
	xml += "</" + name + ">\n";
	return xml;
}
public String toString() {
	if (GrfxMLEngine.DEBUG)
		return (togrfxML(DEFAULT_ID.substring(1)));
	String xml = "<" + DEFAULT_ID.substring(1) + "/>";
	return xml;
}
}
